package com.atguigu.wc;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
import org.apache.flink.util.StringUtils;

/**
 * @author yhm
 * @create 2024-03-30 13:58
 */
public class SocketStreamWordCount1 {
    public static void main(String[] args) throws Exception {
        // TODO1 获取环境
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",8083);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        // 创建本地环境的时候 同时添加一个本地直接能监控的webUI
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",8083);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);



//        StreamExecutionEnvironment remoteEnvironment = StreamExecutionEnvironment.createRemoteEnvironment();

        // 修改flink的批处理和流处理模式
        env.setRuntimeMode(RuntimeExecutionMode.BATCH);

        /*
        flink的并行度优先级
        单个算子紧跟的并行度 > 代码中设置的全局并行度 > 提交任务时设置的并行度 > 配置文件的并行度 > cpu的核数
        实际开发:
        (1) 特殊算子单独设置并行度
        (2) 实际开发优先使用提交任务时设置的并行度 不要在代码中写死全局并行度
        (3) 测试的时候使用代码中设置的全局并行度
         */
        // 设置全局的并行度
        env.setParallelism(3);

        // TODO2 读取数据源
        DataStreamSource<String> streamSource = env.socketTextStream("hadoop102", 7777);

        // TODO3 处理数据
        // 额外添加可作为算子链的操作
        /*
        算子链是flink非常基础经典的优化  任何时候都不推荐禁用
        系统自动识别自动连接为算子链:
            (1)算子之间是one to one的关系
            (2)算子直接的并行度是一样的
         */

        // 任务槽
        // 根据配置文件参数taskmanager.numberOfTaskSlots: 8
        // 确定一个taskManager开启多少个槽
        // 属于静态资源
        // 任务使用槽的个数等于这个任务中算子的最大并行度
        SingleOutputStreamOperator<String> filterStream = streamSource.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String value) throws Exception {
                boolean nullOrWhitespaceOnly = StringUtils.isNullOrWhitespaceOnly(value);
                return !nullOrWhitespaceOnly;
            }
        });

        SingleOutputStreamOperator<String> mapStream = filterStream.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {

                return "单词:" + value;
            }
        });


        SingleOutputStreamOperator<Tuple2<String, Long>> typle2Stream = mapStream.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Long>> out) throws Exception {
                String[] words = value.split(" ");
                for (String word : words) {
                    out.collect(new Tuple2<>(word, 1L));
                }
            }
        })
                // 可以在单独的算子后面添加并行度
                .setParallelism(3);

        SingleOutputStreamOperator<Tuple2<String, Long>> resultStream = typle2Stream
                .keyBy(new KeySelector<Tuple2<String, Long>, String>() {
            @Override
            public String getKey(Tuple2<String, Long> value) throws Exception {
                return value.f0;
            }
        }).sum(1);


        resultStream.print();

        // TODO4 执行环境
        env.execute();
    }
}
